package dblayer;

import static org.junit.Assert.fail;

import java.util.ArrayList;
import java.util.Iterator;

import modellayer.Appointment;
import modellayer.KeyPerson;
import modellayer.ZipCode;

import org.junit.Test;

public class AppointmentDBTest
{
	AppointmentDB aDB = new AppointmentDB();
	
	@Test
	public void insertAppointmentToDB()
	{
		int success = -1;
		
		try
		{
			success = aDB.insertAppointment(new Appointment(-1, "testSubject", 13052014, 23052014, "testAddress", new ZipCode(9000, "Aalborg"), 13052014, new KeyPerson(1)));
		}
		catch (Exception e)
		{
			fail("Failed to insert appointment. Exception: " +e);
		}
		
		if(success == -1)
		{
			fail("Failed to insert appointment into DB");
		}
		else
		{
			System.out.println("Insert comlpeted");
		}
	}
	
	@Test
	public void getAppointmentFromDB()
	{
		ArrayList<Appointment> array = aDB.getAppointments(null);
		
		boolean found = false;
		
		Iterator<Appointment> it = array.iterator();
		
		while (it.hasNext() || !found) {
			if (it.next().getSubject().equals("#1 Appointment")) {
				found = true;
			}
		}
		
		if (!found) {
			fail("The selected Appointment with the specific subject was not found!");
		}
	}
}
